package com.six.firstpage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six.core.entity.Collection;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.firstpage.service.CollectionService;
import com.six.firstpage.mapper.CollectionMapper;
import com.six.firstpage.vo.dto.CollectionGoodsDto;
import com.six.firstpage.vo.dto.CollectionShopDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author ZZH
* @description 针对表【tb_collection】的数据库操作Service实现
* @createDate 2023-03-04 09:45:35
*/
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection>
    implements CollectionService{
    
    private List<Collection> collectionList(){
        //通过token获取用户id
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");

        //根据用户id查询到用户的所有收藏信息
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",tbUserid);
        wrapper.eq("is_deleted",0);
        List<Collection> collections = collectionMapper.selectList(wrapper);
        return collections;
    }

    @Autowired
    private CollectionMapper collectionMapper;

    //查询收藏信息
    @Override
    public Map<String, List<Collection>> getCollection( Integer goodid, Integer shopid) {
        //用户所有的收藏信息--包含店铺和商品
        List<Collection> collections = collectionList();
        Map<String, List<Collection>> map = new HashMap<>();
        if (goodid!=null){
            List<Collection> collectionGood = collections.stream().filter(item ->  item.getGoodsid()!=null&&item.getGoodsid().equals(goodid) ).collect(Collectors.toList());
            map.put("collectionGood",collectionGood);
        }

        if (shopid!=null){
            List<Collection> collectionShop = collections.stream().filter(item -> item.getShopid()!=null&&item.getShopid().equals(shopid)).collect(Collectors.toList());
            map.put("collectionShop",collectionShop);
        }

        return map;
    }

    //添加收藏信息
    @Override
    public Integer addCollection(Collection collection) {
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");

        collection.setUid(tbUserid);
        int insert = collectionMapper.insert(collection);

        return insert;
    }

    //删除收藏信息
    @Override
    public Integer delCollection(Integer id) {
        int i = collectionMapper.deleteById(id);
        return i;
    }

    //用户所有收藏的店铺信息
    @Override
    public List<CollectionShopDto> allCollectionShop() {
        //通过token获取用户id
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");
        List<CollectionShopDto> collectionShopDtos = collectionMapper.allCollectionShop(tbUserid);
        return collectionShopDtos;
    }

    //用户所有收藏的商品信息
    @Override
    public  List<CollectionGoodsDto> allCollectionGoods() {
        //通过token获取用户id
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");
        List<CollectionGoodsDto> collectionGoodsDtos = collectionMapper.allCollectionGoods(tbUserid);
        return collectionGoodsDtos;
    }
}




